import ctypes
import sys
import time

import win32api
import win32con
import pyautogui as ui
import win32gui
from colorama.win32 import windll

if __name__ == '__main__':
    if not ctypes.windll.shell32.IsUserAnAdmin():
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
    number = int(input("你有多少个圣遗物"))
    page_counter = 0
    # win32api.SetCursorPos((150,140))

    handle = win32gui.FindWindow(0, "原神")
    win32gui.SendMessage(handle, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0)  # 这句很重要，这句可以保证将句柄置到前端
    win32gui.SetForegroundWindow(handle)
    x, y, x1, y1 = win32gui.GetWindowRect(handle)
    # win32api.SetCursorPos((150,140))
    x += 3
    y += 32
    time.sleep(0.1)
    pointer = 1
    for a in range(38):
        if pointer > number:
            break
        for i in range(5):
            if pointer > number:
                break
            for z in range(8):
                if pointer > number:
                    break
                win32api.SetCursorPos((x + 150 + 122 * z, y + 140 + i * 146))
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN | win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0)
                print("识别" + str(pointer) + "次")
                pointer += 1
                time.sleep(0.02)
        # 翻页，并且每个三次往回翻一点，因为每个三次就会错开一点
        for i in range(49):
            win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -1)
        time.sleep(0.5)
        if (a + 1) % 3 == 0:
            win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, 1)

